package com.hoperun.product.activity;

import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.hoperun.distributecloth.R;
import com.hoperun.product.base.BaseActivity;
import com.hoperun.product.entity.ContactEntity;
import com.hoperun.product.net.IServiceImpl;
import com.hoperun.product.net.RequestServiceImpl;
import com.hoperun.product.util.ParseUtils;
import com.hoperun.product.view.SimpleDialog;

public class ContactInfoActivity extends BaseActivity {

	private static final String EXTRA_REQUEST_CLOTHTKTNO = "ContactInfoActivity.clothTktNo";

	public static void actionStart(Context context, String clothTktNo) {
		Intent intent = new Intent(context, ContactInfoActivity.class);
		intent.putExtra(EXTRA_REQUEST_CLOTHTKTNO, clothTktNo);
		context.startActivity(intent);
	}

	/** 胚布条码 **/
	private TextView clothTktNoTv;
	/** 布号 **/
	private TextView clothNoTv;
	/** 合约号 **/
	private TextView contactNoTv;
	/** 色组 **/
	private TextView colorGroupTv;
	/** 色号 **/
	private TextView colorNoTv;
	/** 色称 **/
	private TextView colorNameTv;
	/** 计划重量 **/
	private TextView planWeightTv;
	/** 未排程重量 **/
	private TextView noProNumTv;
	/** 未下机匹数 **/
	private TextView noWeightNumTv;
	/** 未下机重量 **/
	private TextView noWeightTv;
	/** 未进仓匹数 **/
	private TextView noStoreNumTv;
	/** 未进仓重量 **/
	private TextView noStoreTv;

	private Button okBtn;

	private String mClothTktNo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact_info_activity);
		initDate();
		initView();
		initEvent();
		getContactByTktNo(mClothTktNo);
	}

	private void initDate() {
		Intent intent = getIntent();
		mClothTktNo = intent.getStringExtra(EXTRA_REQUEST_CLOTHTKTNO);
	}

	private void initView() {

		clothTktNoTv = (TextView) findViewById(R.id.contact_info_clothTktNoTv);
		clothNoTv = (TextView) findViewById(R.id.contact_info_clothNoTv);
		contactNoTv = (TextView) findViewById(R.id.contact_info_contactNoTv);
		colorGroupTv = (TextView) findViewById(R.id.contact_info_colorGroupTv);
		colorNoTv = (TextView) findViewById(R.id.contact_info_colorNoTv);
		colorNameTv = (TextView) findViewById(R.id.contact_info_colorNameTv);
		planWeightTv = (TextView) findViewById(R.id.contact_info_planWeightTv);
		noProNumTv = (TextView) findViewById(R.id.contact_info_noProNumTv);
		noWeightNumTv = (TextView) findViewById(R.id.contact_info_noWeightNumTv);
		noWeightTv = (TextView) findViewById(R.id.contact_info_noWeightTv);
		noStoreNumTv = (TextView) findViewById(R.id.contact_info_noStoreNumTv);
		noStoreTv = (TextView) findViewById(R.id.contact_info_noStoreTv);
		okBtn = (Button) findViewById(R.id.contact_info_okBtn);

		clothTktNoTv.setText(mClothTktNo);
	}

	private void initEvent() {
		okBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ContactInfoActivity.this.finish();
			}
		});
	}

	
	
	
	
	
	
	private void getContactByTktNo(String clothTktNo) {
		showWait();
		RequestServiceImpl.getContactByTktNo(mMIPHandler, IServiceImpl.GET_CONTACT_BY_TKTNO, clothTktNo);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 显示合约信息
	 * @param contact
	 */
	private void showContact(ContactEntity contact) {

		clothNoTv.setText(contact.CLOTHNO);
		contactNoTv.setText(contact.CONTACTNO);
		colorGroupTv.setText(contact.COLORGROUP);
		colorNoTv.setText(contact.COLORNO);
		colorNameTv.setText(contact.COLORNAME);
		planWeightTv.setText(contact.PLANWEIGHT);
		noProNumTv.setText(contact.NOPRONUM);
		noWeightNumTv.setText(contact.NOWEIGHT_NUM);
		noWeightTv.setText(contact.NOWEIGHT);
		noStoreNumTv.setText(contact.NOSTORE_NUM);
		noStoreTv.setText(contact.NOSTORE);
	}

	@Override
	public void onPostHandle(int requestType, Object objBody, boolean error) throws Exception {
		String msg = "";
		String opt = "";
		if (error) {
			switch (requestType) {
			case IServiceImpl.GET_CONTACT_BY_TKTNO:
				try {
					JSONObject jsonObj = new JSONObject((String) objBody);
					msg = jsonObj.optString("MSG_INFO");
					opt = jsonObj.optString("OPT_FLAG");
					if ("0".equals(opt)) {
						ContactEntity contact = ParseUtils.parse(jsonObj.toString(), ContactEntity.class);
						showContact(contact);
					} else if ("1".equals(opt)) {
						SimpleDialog.show(this, msg);
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
				break;

			}
		} else {
			toast(objBody.toString());

		}
		stopWait();

	}

}
